//
//  TriangleLayer.swift
//  QBLaunchAnimation
//
//  Created by 乐启榜 on 16/8/26.
//  Copyright © 2016年 乐启榜. All rights reserved.
//

import UIKit

class TriangleLayer: CAShapeLayer {
    
    let paddingSpace: CGFloat = 30

    override init() {
        super.init()
        
        fillColor = UIColor.colorWithHexString("#da70d6").CGColor
        strokeColor = UIColor.colorWithHexString("#da70d6").CGColor
        lineWidth = 7.0
        lineCap = kCALineCapRound
        lineJoin = kCALineJoinRound
        path = triangleSmallPath.CGPath
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var triangleSmallPath: UIBezierPath {
        let smallPath = UIBezierPath()
        smallPath.moveToPoint(CGPointMake(5.0 + paddingSpace, 95.0))
        smallPath.addLineToPoint(CGPointMake(50.0, 12.5 + paddingSpace))
        smallPath.addLineToPoint(CGPointMake(95.0 - paddingSpace, 95.0))
        smallPath.closePath()
        return smallPath
    }
    
    var leftTrianglePath: UIBezierPath {
        let trianglePath = UIBezierPath()
        trianglePath.moveToPoint(CGPointMake(5.0, 90.0))
        trianglePath.addLineToPoint(CGPointMake(50.0, 12.5 + paddingSpace))
        trianglePath.addLineToPoint(CGPointMake(95.0 - paddingSpace, 95.0))
        trianglePath.closePath()
        return trianglePath
    }
    
    var rightTrianglePath: UIBezierPath {
        let trianglePath = UIBezierPath()
        trianglePath.moveToPoint(CGPointMake(5.0, 95.0))
        trianglePath.addLineToPoint(CGPointMake(50.0, 12.5 + paddingSpace))
        trianglePath.addLineToPoint(CGPointMake(95.0, 95.0))
        trianglePath.closePath()
        return trianglePath
    }
    
    var topTrianglePaht: UIBezierPath {
        let trianglePath = UIBezierPath()
        trianglePath.moveToPoint(CGPointMake(5.0, 95.0))
        trianglePath.addLineToPoint(CGPointMake(50.0, 12.5))
        trianglePath.addLineToPoint(CGPointMake(95.0, 95.0))
        trianglePath.closePath()
        return trianglePath
    }
    
    
    func triangleAnimation() {
        
        let leftAnimation: CABasicAnimation = CABasicAnimation(keyPath: "path")
        leftAnimation.fromValue = triangleSmallPath.CGPath
        leftAnimation.toValue = leftTrianglePath.CGPath
        leftAnimation.beginTime = 0.0
        leftAnimation.duration = 0.3
        
        let rightAnimation: CABasicAnimation = CABasicAnimation(keyPath: "path")
        rightAnimation.fromValue = leftTrianglePath.CGPath
        rightAnimation.toValue = rightTrianglePath.CGPath
        rightAnimation.beginTime = leftAnimation.beginTime + leftAnimation.duration
        rightAnimation.duration = 0.25
        
        let topAnimation: CABasicAnimation = CABasicAnimation(keyPath: "path")
        topAnimation.fromValue = rightTrianglePath.CGPath
        topAnimation.toValue = topTrianglePaht.CGPath
        topAnimation.beginTime = rightAnimation.beginTime + rightAnimation.duration
        topAnimation.duration = 0.20
        
        let triangleAnimationGroup: CAAnimationGroup = CAAnimationGroup()
        triangleAnimationGroup.animations = [leftAnimation,rightAnimation,topAnimation]
        triangleAnimationGroup.duration = topAnimation.beginTime + topAnimation.duration
        triangleAnimationGroup.fillMode = kCAFillModeForwards
        triangleAnimationGroup.removedOnCompletion = false
        addAnimation(triangleAnimationGroup, forKey: nil)
    }

}
